package com.modbus.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.modbus.tool.entity.po.CollectedData;
import com.modbus.tool.entity.po.DataPoint;
import com.modbus.tool.mapper.CollectedDataMapper;
import com.modbus.tool.mapper.DataPointMapper;
import com.modbus.tool.service.IDataPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;

import com.modbus.tool.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-08-07
 * @Description: DataPointServiceImpl
 */
@Service
@RequiredArgsConstructor
public class DataPointServiceImpl extends ServiceImpl<DataPointMapper, DataPoint> implements IDataPointService {

    private final DataPointMapper dataPointMapper;

    private final CollectedDataMapper collectedDataMapper;

    @Override
    public Result<Page<DataPoint>> findPage(Integer pageNum, Integer pageSize, DataPoint dataPoint) {
        Page<DataPoint> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DataPoint> lqw = new LambdaQueryWrapper<>();
        // 添加条件...

        return Result.success(page(page, lqw));
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        if (ids.isEmpty()) {
            return Result.error("请选择要删除的点位");
        }
        // 同时把采集的数据也删除
        collectedDataMapper.delete(
                new LambdaQueryWrapper<CollectedData>()
                        .in(CollectedData::getPointId, ids)
        );
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

    @Override
    @Transactional
    public Boolean addBatch(List<DataPoint> dataPoints) {
        // 有id的修改，无id的添加
        if (dataPoints.isEmpty()) {
            return false;
        }
        dataPoints.forEach(this::saveOrUpdate);
        return true;
    }

}

